package com.ytd.smartcanteen.weighting.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.hjq.language.LocaleContract;
import com.hjq.language.MultiLanguages;
import com.jakewharton.processphoenix.ProcessPhoenix;
import com.ytd.common.util.DeviceUtil;
import com.ytd.common.util.YtdConfig;
import com.ytd.smartcanteen.weighting.MainActivity;
import com.ytd.smartcanteen.weighting.util.FoodManager;

import cn.ytd.smartcanteen.weighting.R;

public class LoginDialog extends PopupWindow {
    MainActivity mActivity;
    EditText edit_Password;
    TextView edit_Name;
    Button btnOK, btnBack;
    Button btnZh, btnEn;
    private final View mMenuView;

    @SuppressLint({"InflateParams", "UseCompatLoadingForDrawables"})
    public LoginDialog(MainActivity context) {
        super(context);
        mActivity = context;
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_login, null);
        edit_Name = mMenuView.findViewById(R.id.edit_login_name);
        edit_Password = mMenuView.findViewById(R.id.edit_login_password);
        mMenuView.findViewById(R.id.ll_show_device).setOnLongClickListener(v -> {
            Toast.makeText(mMenuView.getContext(), DeviceUtil.getDeviceSN(), Toast.LENGTH_LONG).show();
            return false;
        });

        btnOK = mMenuView.findViewById(R.id.btn_login_ok);
        btnZh = mMenuView.findViewById(R.id.btn_language_zh);
        btnEn = mMenuView.findViewById(R.id.btn_language_en);
        btnBack = mMenuView.findViewById(R.id.set_btn_back);
        edit_Name.setText(YtdConfig.getMerchantName());
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        btnOK.setOnClickListener(v -> {
            context.login(edit_Password.getText().toString());
            dismiss();
        });
        btnZh.setOnClickListener(v -> changeLanguageZh());
        btnEn.setOnClickListener(v -> changeLanguageEn());

        btnBack.setOnClickListener(v -> dismiss());
        this.setBackgroundDrawable(context.getResources().getDrawable(android.R.color.transparent));
        this.setContentView(mMenuView);
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        this.setHeight(h - 50);
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        this.setAnimationStyle(R.style.DialogShow);

//        setOnDismissListener(context::refreshLed);
    }

    private void changeLanguageEn() {
        boolean restart = MultiLanguages.setAppLanguage(mActivity, LocaleContract.getEnglishLocale());
        if (restart) {
            mActivity.showProgressDialog(mActivity.getString(R.string.text_configuring),
                false);
            FoodManager.getInstance().initFoodData();
        }
    }

    private void changeLanguageZh() {
        boolean restart = MultiLanguages.setAppLanguage(mActivity, LocaleContract.getSimplifiedChineseLocale());
        if (restart) {
            mActivity.showProgressDialog(mActivity.getString(R.string.text_configuring),
                false);
            FoodManager.getInstance().initFoodData();
        }
    }

    public void Show(int gravity, int x, int y) {
//        LEDManager.getInstance().showViolet();
        showAtLocation(mMenuView, gravity, x, y);
    }

    public void restart() {
        ProcessPhoenix.triggerRebirth(mActivity);
    }
}
